package com.springboot.control;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by mike on 18/3/5.
 */

public class ContextAware implements BeanFactoryPostProcessor,EnvironmentAware {

    ConfigurableEnvironment environment;
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("============================fdsafdsa==========================");
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource e = resourceLoader.getResource("classpath:test.properties");
        ResourcePropertySource rps = null;
        try {
            rps = new ResourcePropertySource(e);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        this.environment.getPropertySources().addLast(rps);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = (ConfigurableEnvironment) environment;
    }
}
